package com.yyk.yiliao.ui.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.NonNull;
import com.google.android.material.appbar.AppBarLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.androidkun.xtablayout.XTabLayout;
import com.bumptech.glide.Glide;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseFragment;
import com.yyk.yiliao.adapter.CommonTabPagerAdapter;
import com.yyk.yiliao.base.BaseRecyclerAdapter;
import com.yyk.yiliao.base.BaseRecyclerHolder;
import com.yyk.yiliao.ui.home.activity.Appointment_Activity;
import com.yyk.yiliao.ui.home.activity.HomeSearch_Activity;
import com.yyk.yiliao.ui.home.activity.Hospitallisting_Activity;
import com.yyk.yiliao.ui.home.activity.Pharmacy_Activity;
import com.yyk.yiliao.ui.gongyong.activity.Reportquery_Activity;
import com.yyk.yiliao.ui.gongyong.activity.WebViewActivity;
import com.yyk.yiliao.ui.activity.detail.activity.Detail_Activity;
import com.yyk.yiliao.util.CheckDecimalPointUtil;
import com.yyk.yiliao.util.GlideImageLoader;

import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.OnClickFlagUtil;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.bean.HomeArticletype_Info;
import com.yyk.yiliao.bean.HomeDepartmentlist_Info;
import com.yyk.yiliao.bean.HomeDiseaselist_Info;
import com.yyk.yiliao.bean.HomeDoctorlist_Info;
import com.yyk.yiliao.bean.HomeGoods_Info;
import com.yyk.yiliao.bean.HomeHomemiddle_Info;
import com.yyk.yiliao.bean.HomeHospitallist_Info;
import com.yyk.yiliao.bean.HomeTitle_Info;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Retrofit;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by Administrator on 2018/6/15 0015.
 * 做一个有高逼咯的马龙
 */

public class Home_Fragment extends BaseFragment {
    private static final String TAG = "Home_Fragment";

    @BindView(R.id.nHome_vp)    //第一个部分中的 导航轮播
            Banner nNHomeVp;
    @BindView(R.id.nHome_vp2)    //第一个部分中的 导航轮播
            ViewPager nNHomeVp2;
    @BindView(R.id.nHome_appointment)  //第二个部分中的 预约挂号，在线药房，报告查询 ,电子病历
            RelativeLayout nHomeAppointment;
    @BindView(R.id.nHome_pharmacy)
    RelativeLayout nHomePharmacy;
    @BindView(R.id.nHome_reportquery)
    RelativeLayout nHomeReportquery;
    @BindView(R.id.nHome_medical)
    LinearLayout nHomeMedical;
    @BindView(R.id.nHome_rv)  //第三个部分中的 医生 推荐列表
            RecyclerView nNHomeRv;
    @BindView(R.id.nHome_rv2)  //第四个部分中的 推荐列表
            RecyclerView nNHomeRv2;
    @BindView(R.id.nHome_card)  //第五个部分中的   鲁医通卡
            ImageView nNHomeCard;
    @BindView(R.id.nHome_location)//第六个部分中的 定位和搜搜
            LinearLayout nHomeLocation;
    @BindView(R.id.nHome_location_tv)
    TextView nHomeLocationTv;
    @BindView(R.id.nHome_healthy) //健康推荐
            RecyclerView nHomeHealthy;
    @BindView(R.id.nHome_top)
    Toolbar nHomeTop;
    @BindView(R.id.nHome_department)//科室推荐
            RecyclerView nHomeDepartment;
    @BindView(R.id.nHome_diseases)//常见疾病
            TagFlowLayout nHomeDiseases;
    @BindView(R.id.tabLayout)
    XTabLayout tabLayout;
    @BindView(R.id.nHome_search)//第七个部分中的搜索
            TextView tvContent;
    @BindView(R.id.nHome_app)
    AppBarLayout nHomeApp;
    @BindView(R.id.nHome_viewFlipper)
    ViewFlipper nHomeViewFlipper;
    @BindView(R.id.nHome_tjimg)
    ImageView nHomeTjimg;

    //^^^^^^^^^^^^^^^^^^^^
    @BindView(R.id.home_top)
    RelativeLayout homeTop;
    @BindView(R.id.home_top2)
    RelativeLayout homeTop2;
    @BindView(R.id.home_top3)
    RelativeLayout homeTop3;
    @BindView(R.id.home_top4)
    LinearLayout homeTop4;
    @BindView(R.id.iv_srys)
    ImageView ivSrys;
    @BindView(R.id.iv_bxfw)
    ImageView ivBxfw;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartRefreshLayout;

    private Retrofit retrofit;
    private BaseRecyclerAdapter<HomeGoods_Info.DataBean> homegoodsAdapter;
    private BaseRecyclerAdapter<HomeDoctorlist_Info.DataBean> nHomeRvAdapter;
    private BaseRecyclerAdapter<HomeHospitallist_Info.DataBean> nHomeRv2Adapter;
    private BaseRecyclerAdapter<HomeDepartmentlist_Info.DataBean> homeDepartmentAdapter;
    private CommonTabPagerAdapter tabAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView() {
        retrofit = RxUtils.getInstance().retrofit;
        initRefresh();
    }

    private void initRefresh() {
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initPost();
                        //禁止点击内容
                        smartRefreshLayout.setDisableContentWhenRefresh(true);
                        smartRefreshLayout.finishRefresh();
                    }
                }, 1500);

            }
        });
    }

    @Override
    public void initData() {
        //获取传回来的值
        iniGetIntent();
        initAdapter();
        setTabXinwen(true);
    }


    /**
     * 地址
     */
    public void iniGetIntent() {
        String poiName = Hawk.get("poiName");
        Logger.e("地址" + poiName);
        if (Hawk.contains("poiName")) {
            if (poiName.equals("") && poiName == null && poiName.equals("null")) {
                nHomeLocationTv.setText("地区");
            } else {
                nHomeLocationTv.setText(poiName);
            }
        } else {
            nHomeLocationTv.setText("地区");
        }

    }

    //如果你需要考虑更好的体验，可以这么操作
    @Override
    public void onStart() {
        super.onStart();
        //开始轮播
        nNHomeVp.startAutoPlay();
        nHomeViewFlipper.startFlipping();

    }

    @Override
    public void onResume() {
        super.onResume();
        initPost();
        iniGetIntent();
    }


    @Override
    public void onStop() {
        super.onStop();
        //结束轮播
        nNHomeVp.stopAutoPlay();
        nHomeViewFlipper.stopFlipping();
    }


    public void initPost() {
        setBanner(nNHomeVp);
        setnHealthy(true);
        setDolist(true);
        setHlist(true);
        setDepartment(true);
        setDiseases(true);
        setHomeviewFlipper(true);
        setHomeHomemiddle(true);

    }

    private void initAdapter() {
        /**
         * 1.
         */
        //设置图片
        homegoodsAdapter = new BaseRecyclerAdapter<HomeGoods_Info.DataBean>(getActivity(), homegoodsList, R.layout.adapter_item_home_healthy) {
            @Override
            public void convert(BaseRecyclerHolder holder, HomeGoods_Info.DataBean item, int position, boolean isScrolling) {
                holder.setText(R.id.b_title, item.getName());
                String money = CheckDecimalPointUtil.checkPoint(item.getMoney());
                holder.setText(R.id.b_ask_price, money);
                //设置图片
                holder.setImageByUrl(R.id.b_picture, ApiService.BASE_URL + item.getPic());
            }
        };
        nHomeHealthy.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        nHomeHealthy.setAdapter(homegoodsAdapter);
        homegoodsAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("id", homegoodsList.get(position).getId());
                startAct(Detail_Activity.class, bundle);
                Logger.e(homegoodsList.get(position).getId() + "");
            }
        });
        /**
         * 2
         */

        /**
         * 3
         */
        nHomeRvAdapter = new BaseRecyclerAdapter<HomeDoctorlist_Info.DataBean>(getActivity(), NHomeRvList, R.layout.adapter_item_home) {
            @Override
            public void convert(BaseRecyclerHolder holder, HomeDoctorlist_Info.DataBean item, int position, boolean isScrolling) {

                holder.setText(R.id.b_true_name, item.getTrue_name());
                int grade = item.getGrade();
                if (grade == 1) {
                    holder.setText(R.id.b_title, "住院医师");
                } else if (grade == 2) {
                    holder.setText(R.id.b_title, "主治医师 ");

                } else if (grade == 3) {
                    holder.setText(R.id.b_title, "副主任医师  ");

                } else if (grade == 2) {
                    holder.setText(R.id.b_title, "主任医师 ");
                }

                //                    String price = CheckDecimalPointUtils.checkPoint(item.getAsk_price());
                holder.setText(R.id.b_ask_price, item.getAsk_price());
                //设置图片
                holder.setImageByUrl(R.id.b_picture, ApiService.BASE_URL + item.getPicture());
            }
        };

        nNHomeRv.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
        //                            nNHomeRv.addItemDecoration(new CustomDecoration(getActivity(), CustomDecoration.HORIZONTAL_LIST, R.drawable.divider_love, 0));
        nNHomeRv.setAdapter(nHomeRvAdapter);
        nHomeRvAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                //医生ID
                int did = NHomeRvList.get(position).getDid();
                Intent intent = new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("toolbar_tv", "医生主页");
                intent.putExtra("url", ApiService.H5_URL_HOSPITALD + did + "&Sources=3");

                //需要用医生did
                int doctor_id = NHomeRvList.get(position).getDid();
                intent.putExtra("doctor_id", doctor_id);
                //把医生名字传过去
                intent.putExtra("doctorname", NHomeRvList.get(position).getTrue_name());
                //www.yunyikang.cn/h5/views/Hospital/DoctorsInfo.html?Sources=2&did=99&from=app
                startActivity(intent);
            }
        });
        /**
         * 4.
         */
        nHomeRv2Adapter = new BaseRecyclerAdapter<HomeHospitallist_Info.DataBean>(getActivity(), NHomeRv2List, R.layout.adapter_item_home2) {
            @Override
            public void convert(BaseRecyclerHolder holder, HomeHospitallist_Info.DataBean item, int position, boolean isScrolling) {
                if (position == 0) {
                    holder.getView(R.id.b_biaozhi).setVisibility(View.VISIBLE);
                } else {
                    holder.getView(R.id.b_biaozhi).setVisibility(View.GONE);
                }
                //医院名字
                holder.setText(R.id.b_hospital_name, item.getHospital_name());

                //医院等级
                int grade = item.getGrade();
                if (grade == 1) {
                    holder.setText(R.id.b_grade, "三级甲等");
                } else if (grade == 2) {
                    holder.setText(R.id.b_grade, "三级医院");
                } else if (grade == 3) {
                    holder.setText(R.id.b_grade, "二级医院");
                } else if (grade == 4) {
                    holder.setText(R.id.b_grade, "一级医院");
                } else if (grade == 5) {
                    holder.setText(R.id.b_grade, "对外综合");
                } else {
                    holder.setText(R.id.b_grade, "对外专科");
                }

                //医院类型
                int hospitalType = item.getHospitalType();
                if (hospitalType == 1) {
                    holder.setText(R.id.b_hospitalType, "综合医院");
                } else if (hospitalType == 2) {
                    holder.setText(R.id.b_hospitalType, "儿童医院");
                } else if (hospitalType == 3) {
                    holder.setText(R.id.b_hospitalType, "传染病医院");
                } else if (hospitalType == 4) {
                    holder.setText(R.id.b_hospitalType, "心血管医院");
                } else if (hospitalType == 5) {
                    holder.setText(R.id.b_hospitalType, "中医医院");
                } else if (hospitalType == 6) {
                    holder.setText(R.id.b_hospitalType, "中医院结合医院");
                } else {
                    holder.setText(R.id.b_hospitalType, "康复医院");
                }
                holder.setText(R.id.b_address, item.getAddress());
                //设置图片
                holder.setImageByUrl(R.id.b_picture, ApiService.BASE_URL + item.getPicture());
            }
        };
        nNHomeRv2.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
        nNHomeRv2.setAdapter(nHomeRv2Adapter);
        nHomeRv2Adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {

                Intent iv_bxfw = new Intent(getActivity(), Hospitallisting_Activity.class);
                iv_bxfw.putExtra("hid", NHomeRv2List.get(position).getHid());
                iv_bxfw.putExtra("title", NHomeRv2List.get(position).getHospital_name());
                startActivity(iv_bxfw);
            }
        });
        /**
         * 5.
         */
        homeDepartmentAdapter = new BaseRecyclerAdapter<HomeDepartmentlist_Info.DataBean>(getActivity(), HomeDepartmentList, R.layout.item_flowlayout_department) {
            @Override
            public void convert(BaseRecyclerHolder holder, HomeDepartmentlist_Info.DataBean item, int position, boolean isScrolling) {
                TextView tv = holder.getView(R.id.tv_hot);
                if (position == 0) {
                    tv.setVisibility(View.VISIBLE);
                }
                if (position == 1) {
                    tv.setVisibility(View.VISIBLE);

                }
                holder.setText(R.id.tv, item.getDepartment_name());
            }
        };
        nHomeDepartment.setLayoutManager(new GridLayoutManager(this.getContext(), 4, GridLayoutManager.VERTICAL, false));
        nHomeDepartment.setAdapter(homeDepartmentAdapter);
        homeDepartmentAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {

            }
        });
        /**
         * 6
         */
        //推荐病种
        final LayoutInflater mInflater = LayoutInflater.from(getContext());
        nHomeDiseases.setSelected(true);
        nHomeDiseases.setAdapter(new TagAdapter<HomeDiseaselist_Info.DataBean>(homeDiseaseList) {
            @Override
            public View getView(FlowLayout parent, final int position, HomeDiseaselist_Info.DataBean s) {
                final TextView textView = (TextView) mInflater.inflate(R.layout.item_flowlayout_diseases,
                        nHomeDiseases, false);
                textView.setText(s.getDisease_name());
                return textView;
            }
        });
        nHomeDiseases.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                //                Intent intent = new Intent(context, PaySuccess_Activity.class);
                //                context.startActivity(intent);
                //                    Toast.makeText(getContext(), data.get(position).getDisease_name() + "", Toast.LENGTH_LONG).show();
                return false;
            }
        });
        nHomeDiseases.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {

            }
        });
        /**
         * 7.
         */


    }


    List<HomeGoods_Info.DataBean> homegoodsList = new ArrayList<>();
    List<HomeTitle_Info.DataBean> ViewFlipperList = new ArrayList<>();
    List<HomeDoctorlist_Info.DataBean> NHomeRvList = new ArrayList<>();
    List<HomeHospitallist_Info.DataBean> NHomeRv2List = new ArrayList<>();
    List<HomeDepartmentlist_Info.DataBean> HomeDepartmentList = new ArrayList<>();
    List<HomeDiseaselist_Info.DataBean> homeDiseaseList = new ArrayList<>();
    List<HomeArticletype_Info.DataBean> homeArticlettypeList = new ArrayList<>();


    public void onSuccess(Object info) {

        if (info instanceof HomeGoods_Info) {
            if (((HomeGoods_Info) info).getData().size() > 0 && ((HomeGoods_Info) info).getData() != null) {
                homegoodsList.addAll(((HomeGoods_Info) info).getData());
            }
            homegoodsAdapter.notifyDataSetChanged();
        }

        if (info instanceof HomeHomemiddle_Info) {
            //            Logger.i("首页医院推荐图:" + info.toString());
            String pic = ((HomeHomemiddle_Info) info).getData().get(0).getPic();
            Glide.with(getActivity()).load(ApiService.BASE_URL + pic).into(nHomeTjimg);
        }

        if (info instanceof HomeDoctorlist_Info) {
            //            Logger.i("医生推荐:" + info.toString());
            final List<HomeDoctorlist_Info.DataBean> data = ((HomeDoctorlist_Info) info).getData();
            //                    String price = CheckDecimalPointUtils.checkPoint(item.getAsk_price());
            if (((HomeDoctorlist_Info) info).getData() != null && ((HomeDoctorlist_Info) info).getData().size() > 0) {
                NHomeRvList.addAll(((HomeDoctorlist_Info) info).getData());
            }
            nHomeRvAdapter.notifyDataSetChanged();
        }
        if (info instanceof HomeHospitallist_Info) {
            if (((HomeHospitallist_Info) info).getData() != null && ((HomeHospitallist_Info) info).getData().size() > 0) {
                NHomeRv2List.addAll(((HomeHospitallist_Info) info).getData());
            }
            nHomeRv2Adapter.notifyDataSetChanged();
        }

        if (info instanceof HomeDepartmentlist_Info) {
            //            Logger.i("科室推荐:" + info.toString());
            if (((HomeDepartmentlist_Info) info).getData() != null && ((HomeDepartmentlist_Info) info).getData().size() > 0) {
                HomeDepartmentList.addAll(((HomeDepartmentlist_Info) info).getData());
            }
            homeDepartmentAdapter.notifyDataSetChanged();

        }
        if (info instanceof HomeDiseaselist_Info) {
            if (((HomeDiseaselist_Info) info).getData() != null && ((HomeDiseaselist_Info) info).getData().size() > 0) {
                homeDiseaseList.addAll(((HomeDiseaselist_Info) info).getData());
            }
            nHomeDiseases.onChanged();

        }
        if (info instanceof HomeArticletype_Info) {
            if (((HomeArticletype_Info) info).getData() != null && ((HomeArticletype_Info) info).getData().size() > 0) {
                homeArticlettypeList.addAll(((HomeArticletype_Info) info).getData());
            }
            //底部新闻列表
            List<String> string = new ArrayList<>();
            final List<String> integers = new ArrayList<>();
            string.add("推荐");
            integers.add("0");
            for (int i = 0; i < homeArticlettypeList.size(); i++) {
                string.add(homeArticlettypeList.get(i).getName());
                integers.add(String.valueOf(homeArticlettypeList.get(i).getId()));
            }
            Logger.e("zhoukang1_" + integers.size());
            Logger.e("zhoukang2_" + string.size());
            tabAdapter = new CommonTabPagerAdapter(getChildFragmentManager()
                    , string.size(), string, getActivity());
            tabAdapter.setListener(new CommonTabPagerAdapter.TabPagerListener() {
                @Override
                public Fragment getFragment(int position) {
                    HomeTab_Fragment fragment = HomeTab_Fragment.newInstance(position);
                    Bundle bundle = new Bundle();
                    bundle.putString(HomeTab_Fragment.ARGS_PAGE, integers.get(position) + "");
                    fragment.setArguments(bundle);
                    return fragment;
                }
            });
            nNHomeVp2.setAdapter(tabAdapter);
            tabLayout.setupWithViewPager(nNHomeVp2);
            nNHomeVp2.setCurrentItem(0);
        }

    }


    /**
     * 请求中的时候
     */
    private void onComplete() {

    }

    /**
     * 健康推荐
     *
     * @param标识这个接口的名字
     */
    public void setnHealthy(final boolean flag) {
        Map<String, String> map = new TreeMap<>();

        map.put("sign", Md5Util2.createSign(map));
        retrofit.create(ApiService.class).postHomeDrug(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<HomeGoods_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                        onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();


                    }

                    @Override
                    public void onNext(HomeGoods_Info find_info) {
                        if (find_info.getCode() == 1) {
                            if (flag) {
                                homegoodsList.clear();
                            }
                            onSuccess(find_info);//登录成功------获取完数据,返回给P---P获取到数据之后就将数据交回给V
                        }
                    }
                });

    }


    public void setDolist(final boolean flag) {
        Map<String, String> map = new TreeMap<>();
        map.put("sign", Md5Util2.createSign(map));
        retrofit.create(ApiService.class).postHomeDoctorlist(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<HomeDoctorlist_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                        onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();

                    }

                    @Override
                    public void onNext(HomeDoctorlist_Info find_info) {
                        if (find_info.getCode() == 1) {
                            if (flag) {
                                NHomeRvList.clear();
                            }
                            onSuccess(find_info);
                        }
                    }


                });

    }


    public void setHlist(final boolean flag) {
        Map<String, String> map = new TreeMap<>();

        map.put("sign", Md5Util2.createSign(map));
        retrofit.create(ApiService.class).postHomeHospitallist(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<HomeHospitallist_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                        onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(HomeHospitallist_Info info) {
                        Logger.i("医院推荐:" + info.toString());
                        if (info.getCode() == 1) {
                            if (flag) {
                                NHomeRv2List.clear();
                            }
                            onSuccess(info);
                        }
                    }

                });

    }


    public void setDepartment(final boolean flag) {
        Map<String, String> map = new TreeMap<>();

        map.put("sign", Md5Util2.createSign(map));
        retrofit.create(ApiService.class).postHomeDepartmentlist(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<HomeDepartmentlist_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                        onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(HomeDepartmentlist_Info info) {
                        if (info.getCode() == 1) {
                            if (flag) {
                                HomeDepartmentList.clear();
                            }
                            onSuccess(info);
                        }
                    }


                });


    }


    public void setDiseases(final boolean flag) {

        Map<String, String> map = new TreeMap<>();

        map.put("sign", Md5Util2.createSign(map));
        retrofit.create(ApiService.class).postHomeDiseaselist(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<HomeDiseaselist_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                        onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(HomeDiseaselist_Info info) {
                        if (info.getCode() == 1) {
                            if (flag) {
                                homeDiseaseList.clear();
                            }
                            onSuccess(info);
                        }
                    }


                });


    }


    public void setBanner(Banner nNHomeVp) {
        List<Integer> images2 = new ArrayList<>();
        images2.add(R.mipmap.banner1);
        images2.add(R.mipmap.banner2);

        images2.add(R.mipmap.banner3);

        nNHomeVp.setImages(images2);
        //设置图片加载器
        nNHomeVp.setImageLoader(new GlideImageLoader());
        //设置图片集合
        //banner设置方法全部调用完毕时最后调用
        nNHomeVp.start();
    }

    /**
     * 设置底部tab
     *
     * @param flag
     */
    public void setTabXinwen(final boolean flag) {

        Map<String, String> map = new TreeMap<>();

        map.put("sign", Md5Util2.createSign(map));
        retrofit.create(ApiService.class).postHomeArticletype(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<HomeArticletype_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                        onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();


                    }

                    @Override
                    public void onNext(HomeArticletype_Info find_info) {
                        if (find_info.getCode() == 1) {
                            if (flag) {
                                homeArticlettypeList.clear();
                            }
                            onSuccess(find_info);
                        }
                    }


                });


    }

    /**
     * 设置健康头条
     *
     * @param flag
     */
    public void setHomeviewFlipper(final boolean flag) {
        Map<String, String> map = new TreeMap<>();

        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postHomeTitle(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<HomeTitle_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                        onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();

                    }

                    @Override
                    public void onNext(HomeTitle_Info info) {
                        if (info.getCode() == 1) {
                            if (flag) {
                                ViewFlipperList.clear();
                            }
                            Logger.i("健康头条:" + info.toString());
                            if (((HomeTitle_Info) info).getData() != null && ((HomeTitle_Info) info).getData().size() > 0) {
                                ViewFlipperList.addAll(((HomeTitle_Info) info).getData());
                            }
                            for (int i = 0; i < ViewFlipperList.size(); i++) {
                                if (i % 2 == 0) {
                                    View view = View.inflate(getContext(), R.layout.viewflipper_one, null);
                                    TextView tv1 = (TextView) view.findViewById(R.id.tv);
                                    tv1.setText(ViewFlipperList.get(i).getTitle());
                                    final HomeTitle_Info.DataBean dataBean = ViewFlipperList.get(i);
                                    tv1.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Intent intent = new Intent(getActivity(), WebViewActivity.class);
                                            intent.putExtra("toolbar_tv", "发现详情");
                                            intent.putExtra("url", ApiService.H5_URL_NEWDISCOVER + dataBean.getId() + "&Sources=3?from=app");
                                            startActivity(intent);
                                        }
                                    });
                                    TextView tv2 = (TextView) view.findViewById(R.id.tv2);
                                    tv2.setText(ViewFlipperList.get(i + 1).getTitle());
                                    final HomeTitle_Info.DataBean dataBean2 = ViewFlipperList.get(i + 1);
                                    tv2.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Intent intent = new Intent(getActivity(), WebViewActivity.class);
                                            intent.putExtra("toolbar_tv", "发现详情");
                                            intent.putExtra("url", ApiService.H5_URL_NEWDISCOVER + dataBean2.getId() + "&Sources=3?from=app");
                                            startActivity(intent);
                                        }
                                    });
                                    nHomeViewFlipper.addView(view);
                                }
                            }


                        }
                    }


                });


    }

    /**
     * 首页医院推荐图
     *
     * @param flag
     */
    public void setHomeHomemiddle(final boolean flag) {
        Map<String, String> map = new TreeMap<>();
        map.put("typeid", 2 + "");

        map.put("sign", Md5Util2.createSign(map));
        retrofit.create(ApiService.class).postHomeHomemiddle(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<HomeHomemiddle_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                        onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();


                    }

                    @Override
                    public void onNext(HomeHomemiddle_Info info) {
                        if (info.getCode() == 1) {
                            //不需要优化
                            onSuccess(info);//登录成功------获取完数据,返回给P---P获取到数据之后就将数据交回给V
                        }
                    }


                });

    }


    /**
     * 初始化点击事件
     *
     * @param view
     */
    @OnClick({R.id.home_top, R.id.home_top2, R.id.home_top3, R.id.home_top4, R.id.iv_srys, R.id.iv_bxfw,
            R.id.nHome_location, R.id.nHome_search, R.id.nHome_appointment, R.id.nHome_pharmacy, R.id.nHome_reportquery, R.id.nHome_card, R.id.nHome_medical})
    public void onViewClicked(View view) {
        if (OnClickFlagUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.nHome_location:
                //TODO 定位筛选
//                Intent intent = new Intent(getActivity(), Location_Activity.class);
//                startActivity(intent);

                break;
            case R.id.nHome_search:
                //TODO 搜索界面
                startActivity(new Intent(getActivity(), HomeSearch_Activity.class));
                break;
            case R.id.nHome_appointment:
                //TODO 预约挂号
                String locationTv = nHomeLocationTv.getText().toString();
                Intent intent2 = new Intent(getActivity(), Appointment_Activity.class).putExtra("locationTv", locationTv);
                startActivity(intent2);
                break;
            case R.id.nHome_pharmacy:
                //TODO 在线药房
                Intent intent1 = new Intent(getActivity(), Pharmacy_Activity.class);
                startActivity(intent1);

                break;
            case R.id.nHome_reportquery:
                //TODO 报告查询
                startActivity(new Intent(getActivity(), Reportquery_Activity.class).putExtra("caonima", "在线问诊"));

                break;
            case R.id.nHome_card:
                //TODO 鲁亿通卡办理
                Intent intent3 = new Intent(getActivity(), WebViewActivity.class);
                intent3.putExtra("toolbar_tv", "鲁医通卡预约办理");
                intent3.putExtra("url", ApiService.H5_URL + "views/Login/Cardlogin.html?from=app&Sources=3");
                startActivity(intent3);

                break;
            case R.id.nHome_medical:
                //TODO 健康体检
                Intent nHome_medical = new Intent(getActivity(), WebViewActivity.class);
                nHome_medical.putExtra("toolbar_tv", "健康体检");
                nHome_medical.putExtra("url", ApiService.H5_URL + "views/Tabs/checkup.html?from=app" + "&Sources=3");
                startActivity(nHome_medical);
                break;
            case R.id.home_top:
                startActivity(new Intent(getActivity(), Reportquery_Activity.class).putExtra("caonima", "健康数据"));

                break;
            case R.id.home_top2:
                startActivity(new Intent(getActivity(), Reportquery_Activity.class).putExtra("caonima", "报告检查"));

                break;
            case R.id.home_top3:
                Intent home_top3 = new Intent(getActivity(), WebViewActivity.class);
                home_top3.putExtra("toolbar_tv", "慢病管理");
                home_top3.putExtra("url", ApiService.H5_URL + "views/Tabs/chronic.html?from=app" + "&Sources=3");
                startActivity(home_top3);
                break;
            case R.id.home_top4:
                Intent home_top4 = new Intent(getActivity(), WebViewActivity.class);
                home_top4.putExtra("toolbar_tv", "症状自检");
                home_top4.putExtra("url", ApiService.H5_URL + "views/Tabs/symptom.html?from=app" + "&Sources=3");
                startActivity(home_top4);
                break;
            case R.id.iv_srys:
                Intent iv_srys = new Intent(getActivity(), WebViewActivity.class);
                iv_srys.putExtra("toolbar_tv", "私人医生");
                iv_srys.putExtra("url", ApiService.H5_URL + "views/Tabs/privatedoctor.html?from=app" + "&Sources=3");
                startActivity(iv_srys);
                break;
            case R.id.iv_bxfw:
                Intent iv_bxfw = new Intent(getActivity(), WebViewActivity.class);
                iv_bxfw.putExtra("toolbar_tv", "保险服务");
                iv_bxfw.putExtra("url", ApiService.H5_URL + "views/Tabs/insurance.html?from=app" + "&Sources=3");
                startActivity(iv_bxfw);
                break;
        }
    }


}
